import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //加载动画的配置
    loadingConfig:{
      isShowLoading:false,
      loadingText:""
    },
    //底部菜单的配置
    actionSheetConfig:{
      isShowActionSheet:false,
      items:[],
      isShowCancel:false,
      callBack:null
    },
    //预览图片的配置
    previewImageConfig:{
      isShowPreviewImage:false,
      urls:[],
      current:"",
      currentIndex:0
    }
  },
  mutations: {
    //显示加载动画
    showLoading(state,config){
      state.loadingConfig.isShowLoading=true;
      if(config&&config.content){
        if(config.content.length>6){
          throw new Error("loading的文字不能超过6个汉字");
        }
        state.loadingConfig.loadingText=config.content;
      }
    },
    //隐藏加载动画
    hideLoading(state){
      state.loadingConfig.isShowLoading=false;
    },
    //显示底部菜单
    showActionSheet(state,config){
      state.actionSheetConfig.isShowActionSheet=true;
      if(config&&config.items&&config.items.length>0){
        state.actionSheetConfig.items=config.items;
      }
      else{
        throw new Error("actionSheet配置config.items出现了错误");
      }
      if(config&&config.success&&typeof config.success=="function"){
        state.actionSheetConfig.callBack=config.success;
      }
    },
    //隐藏底部菜单
    hideActionSheet(state){
      state.actionSheetConfig.isShowActionSheet=false;
    },
    //显示预览图片
    showPreviewImage(state,config){
      state.previewImageConfig.isShowPreviewImage=true;
      if(config&&config.urls&&config.urls.length>0){
        state.previewImageConfig.urls=config.urls;
      }
      else{
        throw new Error("previewImage没有定义预览图片");
      }
      if(config&&config.current){
        state.previewImageConfig.current=config.current;
        let currentIndex=state.previewImageConfig.urls.indexOf(config.current);
        if(currentIndex!=-1){
          state.previewImageConfig.currentIndex=currentIndex;
        }
        else{
          state.previewImageConfig.currentIndex=0;
        }
      }
    },
    //隐藏预览图片
    hidePreviewImage(state){
      state.previewImageConfig.isShowPreviewImage=false;
      state.previewImageConfig.current="";
      state.previewImageConfig.currentIndex=0;
    }
  },
  actions: {
    showLoading({commit},config){
      commit("showLoading",config);
    },
    hideLoading({commit}){
      commit("hideLoading");
    },
    showActionSheet({commit},config){
      commit("showActionSheet",config);
    },
    hideActionSheet({commit}){
      commit("hideActionSheet");
    },
    showPreviewImage({commit},config){
      commit("showPreviewImage",config);
    },
    hidePreviewImage({commit}){
      commit("hidePreviewImage");
    }
  },
  getters:{
    isShowLoading(state){
      return state.loadingConfig.isShowLoading;
    },
    loadingText(state){
      return state.loadingConfig.loadingText?state.loadingConfig.loadingText:"";
    },
    isShowActionSheet(state){
      return state.actionSheetConfig.isShowActionSheet;
    },
    actionSheetItems(state){
      return state.actionSheetConfig.items;
    },
    actionSheetCallBack(state){
      return state.actionSheetConfig.callBack;
    },
    isShowPreviewImage(state){
      return state.previewImageConfig.isShowPreviewImage;
    },
    previewImageUrls(state){
      return state.previewImageConfig.urls;
    },
    previewImageCurrentIndex(state){
      return state.previewImageConfig.currentIndex;
    }
  }
})
